package com.model.dao;

//Generated Sep 16, 2013 2:49:26 PM by Hibernate Tools 4.0.0

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * Home object for domain model class ChildcatalogTable.
 * 
 * @see com.model.dao.ChildcatalogTable
 * @author Hibernate Tools
 */
@Stateless
public class ChildcatalogTableDAO {

	private static final Log log = LogFactory
			.getLog(ChildcatalogTableDAO.class);

	Session session;
	Transaction tx;

	public void persist(ChildcatalogTable transientInstance) throws Exception {
		log.debug("persisting ChildcatalogTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}

	public void remove(ChildcatalogTable persistentInstance) throws Exception {
		log.debug("removing ChildcatalogTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}

	public ChildcatalogTable merge(ChildcatalogTable detachedInstance) {
		log.debug("merging ChildcatalogTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			ChildcatalogTable result = (ChildcatalogTable) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
//			throw ex;
		}
		return detachedInstance;
	}

	public ChildcatalogTable findById(int id) {
		log.debug("getting ChildcatalogTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			ChildcatalogTable instance = (ChildcatalogTable) session.get(
					ChildcatalogTable.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
//			throw ex;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<ChildcatalogTable> findByAll() {
		log.debug("getting ChildcatalogTable instance ");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
//			/Query query = session.createQuery("select distinct ot from OrderTable ot Join fetch ot.orderItemsTables oit  join fetch oit.childcatalogTable cct join fetch cct.mastercatalogTable where orderstatus <> 'Delivered'");
			Query query = session.createQuery("select distinct  cct from ChildcatalogTable cct left Join fetch cct.inventoryTable ");
			List<ChildcatalogTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
//			throw ex;
		}
		return null;
	}
}
